#include <stdio.h>
#include "Log.h"
#include "Xml.h"
#include "XmlTest.h"

XmlTest::XmlTest(void)
{

}

XmlTest::~XmlTest( void )
{

}
 
bool XmlTest::Test()
{
	printf("=================XmlTest=================\n\n");
	printf("Test Read Xml File\n");
	Xml xml;
	if ( !xml.LoadFile("Config/biaoqing.xml", ENCODING_UNKWON) )
		ProccessError(LOGERROR, "LoadFile biaoqing.xml", false);
	printf("name:%s\n", xml.Name());

	if ( !xml.FirstChild() )
		ProccessError(LOGERROR, "FirstChild", false);
 	printf("name:%s\n", xml.Name());

	if ( !xml.Parent() )
		ProccessError(LOGERROR, "Parent", false);
	printf("name:%s\n", xml.Name());

	if ( !xml.FirstChild() )
		ProccessError(LOGERROR, "FirstChild", false);
	printf("name:%s\n", xml.Name());

	for ( size_t i = 0; i < 300; i++ )
	{
		int id = 0;
		if ( !xml.GetIntAttr("id", id) )
			ProccessError(LOGERROR, "GetIntAttr id", false);
		printf("i:%d id:%d ", i, id);
		int icon = 0;
		if ( !xml.GetIntAttr("icon", icon) )
			ProccessError(LOGERROR, "GetIntAttr icon", false);
		printf("icon:%d ", icon);
		char buff[64] = "";
		if ( !xml.GetStringAttr("desc", buff, 64) )
			ProccessError(LOGERROR, "GetStringAttr desc", false);
		printf("desc:%s\n", buff);
		if ( !xml.Next() )
			break;
	}

	if ( !xml.Parent() )
		ProccessError(LOGERROR, "LastChild", false);
	printf("name:%s\n", xml.Name());

	if ( !xml.LastChild() )
		ProccessError(LOGERROR, "LastChild", false);
	printf("name:%s\n", xml.Name());

	printf("Test Write Xml File\n");
	Xml Tmp;
	Tmp.InitXml("Root", true, ENCODING_UNKWON);
	printf("name:%s\n", Tmp.Name());
	Tmp.AddChild("lele");
	Tmp.AddAttr("name","lele");
	Tmp.AddAttr("code",1024);
	Tmp.AddChild("lele");
	Tmp.AddAttr("name","lele");
	Tmp.AddAttr("code",1024);
	Tmp.AddSibling("lele");
	Tmp.AddAttr("name","lele");
	Tmp.AddAttr("code",1024);
	Tmp.AddSibling("lele");
	Tmp.AddAttr("name","lele");
	Tmp.AddAttr("code",1024);
	Tmp.SetString("lele123456");
	printf("name:%s\n", Tmp.Name());
	Tmp.SaveFile("Config/write.xml");
	printf("=================XmlTest=================\n\n");
	return true;
}
